//
//  MineFocusViewController.m
//  huashiedu
//
//  Created by oceans on 2018/8/31.
//  Copyright © 2018年 hema. All rights reserved.
//

#import "MineFocusViewController.h"
#import "SchoolListTableViewCell.h"
#import "TeacherListTableViewCell.h"
#import "HttpRequest.h"
#import <UIImageView+WebCache.h>
#import "TeacherModel.h"
#import "SchoolListModel.h"
#import "TeacherDetailViewController.h"
#import "NormalWebViewController.h"
#import <MJRefresh.h>

@interface MineFocusViewController () <UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UIButton *schoolButton;
@property (strong, nonatomic) UIButton *teacherButton;
@property (strong, nonatomic) CALayer *lineLayer;
@property (strong, nonatomic) UITableView *schoolTable;
@property (strong, nonatomic) UITableView *teacherTable;
@property (strong, nonatomic) NSMutableArray <SchoolListModel *> *schoolArr;
@property (strong, nonatomic) NSMutableArray <TeacherModel *> *teacherArr;
@property (strong, nonatomic) MetaModel *schoolModel;
@property (strong, nonatomic) MetaModel *teacherModel;
@end

@implementation MineFocusViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setupview];
    [self loadSchoolData];
}

- (void)loadSchoolData {
    
    self.schoolArr = [NSMutableArray array];
    __weak typeof(self) weakself = self;
    [HttpRequest getURL:kfocus_school paras:nil showProgress:NO successful:^(id  _Nullable result) {
        for (NSDictionary *dic in result[@"data"][@"items"]) {
            SchoolListModel *model = [[SchoolListModel alloc] initWithDic:dic];
            [weakself.schoolArr addObject:model];
        }
        [weakself.schoolTable reloadData];
        MetaModel *model = [[MetaModel alloc] initWithDic:result[@"data"][@"meta"]];
        if (model.currentPage == model.pageCount || model.pageCount == 0) {
            [weakself.schoolTable.footer endRefreshingWithNoMoreData];
        }else {
            weakself.schoolModel = model;
        }
    } failed:nil];
}

- (void)loadMoreDataSchool {
    
    __weak typeof(self) weakself = self;
    [HttpRequest getURL:kfocus_school paras:@{@"page": [NSNumber numberWithInt:self.schoolModel.currentPage + 1]} showProgress:NO successful:^(id  _Nullable result) {
        for (NSDictionary *dic in result[@"data"][@"items"]) {
            SchoolListModel *model = [[SchoolListModel alloc] initWithDic:dic];
            [weakself.schoolArr addObject:model];
        }
        [weakself.schoolTable reloadData];
        MetaModel *model = [[MetaModel alloc] initWithDic:result[@"data"][@"meta"]];
        if (model.currentPage == model.pageCount || model.pageCount == 0) {
            [weakself.schoolTable.footer endRefreshingWithNoMoreData];
        }else {
            weakself.schoolModel = model;
            [weakself.schoolTable.footer endRefreshing];
        }
    } failed:^(int code, NSString *errMsg) {
        [weakself.schoolTable.footer endRefreshing];
    }];
}

- (void)loadTeacherData {
    
    self.teacherArr = [NSMutableArray array];
    __weak typeof(self) weakself = self;
    [HttpRequest getURL:kfocus_teacher paras:nil showProgress:NO successful:^(id  _Nullable result) {
        for (NSDictionary *dic in result[@"data"][@"items"]) {
            TeacherModel *model = [[TeacherModel alloc] initWithDic:dic];
            [weakself.teacherArr addObject:model];
        }
        [weakself.teacherTable reloadData];
        MetaModel *model = [[MetaModel alloc] initWithDic:result[@"data"][@"meta"]];
        if (model.currentPage == model.pageCount || model.pageCount == 0) {
            [weakself.teacherTable.footer endRefreshingWithNoMoreData];
        }else {
            weakself.teacherModel = model;
        }
    } failed:nil];
}

- (void)loadMoreDataTeacher {
    
    __weak typeof(self) weakself = self;
    [HttpRequest getURL:kfocus_teacher paras:@{@"page": [NSNumber numberWithInt:self.teacherModel.currentPage + 1]} showProgress:NO successful:^(id  _Nullable result) {
        for (NSDictionary *dic in result[@"data"][@"items"]) {
            TeacherModel *model = [[TeacherModel alloc] initWithDic:dic];
            [weakself.teacherArr addObject:model];
        }
        [weakself.teacherTable reloadData];
        MetaModel *model = [[MetaModel alloc] initWithDic:result[@"data"][@"meta"]];
        if (model.currentPage == model.pageCount || model.pageCount == 0) {
            [weakself.teacherTable.footer endRefreshingWithNoMoreData];
        }else {
            weakself.teacherModel = model;
            [weakself.teacherTable.footer endRefreshing];
        }
    } failed:^(int code, NSString *errMsg) {
        [weakself.teacherTable.footer endRefreshing];
    }];
}

- (void)setupview {
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"我的关注";
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, XNavigationHeight, ScreenWidth, 42)];
    headerView.backgroundColor = [UIColor whiteColor];
    CALayer *line = [[CALayer alloc] init];
    line.backgroundColor = [UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1.0].CGColor;
    line.frame = CGRectMake(0, 41.5, ScreenWidth, 0.5);
    [headerView.layer addSublayer:line];
    [self.view addSubview:headerView];
    
    self.schoolButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _schoolButton.frame = CGRectMake(0, 0, 100, 42);
    _schoolButton.tag = 0;
    [_schoolButton addTarget:self action:@selector(headerButtonsPressed:) forControlEvents:UIControlEventTouchUpInside];
    _schoolButton.center = CGPointMake(ScreenWidth * 0.25, 21);
    [_schoolButton setTitle:@"精品学校" forState:UIControlStateNormal];
    [_schoolButton setTitleColor:COLOR(63, 63, 63, 1) forState:UIControlStateNormal];
    [_schoolButton setTitleColor:COLOR(255, 36, 69, 1) forState:UIControlStateSelected];
    _schoolButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
    _schoolButton.selected = YES;
    [headerView addSubview:_schoolButton];
    
    self.teacherButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _teacherButton.frame = CGRectMake(0, 0, 100, 42);
    _teacherButton.tag = 1;
    [_teacherButton addTarget:self action:@selector(headerButtonsPressed:) forControlEvents:UIControlEventTouchUpInside];
    _teacherButton.center = CGPointMake(ScreenWidth * 0.75, 21);
    [_teacherButton setTitle:@"明星老师" forState:UIControlStateNormal];
    [_teacherButton setTitleColor:COLOR(63, 63, 63, 1) forState:UIControlStateNormal];
    [_teacherButton setTitleColor:COLOR(255, 36, 69, 1) forState:UIControlStateSelected];
    _teacherButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [headerView addSubview:_teacherButton];
    
    self.lineLayer = [[CALayer alloc] init];
    _lineLayer.frame = CGRectMake(0, 0, 15, 4);
    _lineLayer.backgroundColor = [UIColor colorWithRed:255/255.0 green:36/255.0 blue:69/255.0 alpha:1.0].CGColor;
    _lineLayer.cornerRadius = 2.0;
    _lineLayer.masksToBounds = YES;
    [headerView.layer addSublayer:_lineLayer];
    _lineLayer.position = CGPointMake(_schoolButton.center.x, 37.5);
    
    self.schoolTable = [[UITableView alloc] initWithFrame:CGRectMake(0, XNavigationHeight + 42, ScreenWidth, ScreenHeight - XNavigationHeight - 42) style:UITableViewStylePlain];
    _schoolTable.delegate = self;
    _schoolTable.dataSource = self;
    [_schoolTable registerNib:[UINib nibWithNibName:@"SchoolListTableViewCell" bundle:nil] forCellReuseIdentifier:@"SchoolListTableViewCell"];
    [self.view addSubview:_schoolTable];
    MJRefreshAutoStateFooter *footer = [MJRefreshAutoStateFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreDataSchool)];
    footer.automaticallyHidden = NO;
    _schoolTable.footer = footer;
    _schoolTable.tableFooterView = [UIView new];
    
    self.teacherTable = [[UITableView alloc] initWithFrame:CGRectMake(0, XNavigationHeight + 42, ScreenWidth, ScreenHeight - XNavigationHeight - 42) style:UITableViewStylePlain];
    _teacherTable.delegate = self;
    _teacherTable.dataSource = self;
    _teacherTable.tag = 1;
    _teacherTable.hidden = YES;
    [_teacherTable registerNib:[UINib nibWithNibName:@"TeacherListTableViewCell" bundle:nil] forCellReuseIdentifier:@"TeacherListTableViewCell"];
    [self.view addSubview:_teacherTable];
    _teacherTable.tableFooterView = [UIView new];
    MJRefreshAutoStateFooter *footer1 = [MJRefreshAutoStateFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreDataTeacher)];
    footer1.automaticallyHidden = NO;
    _teacherTable.footer = footer1;
}

- (void)headerButtonsPressed:(UIButton*)sender {
    if (sender.tag == 1) {
        _schoolButton.selected = NO;
        _teacherButton.selected = YES;
        _schoolTable.hidden = YES;
        _teacherTable.hidden = NO;
        if (_teacherArr == nil) {
            [self loadTeacherData];
        }
    }else {
        _schoolButton.selected = YES;
        _teacherButton.selected = NO;
        _schoolTable.hidden = NO;
        _teacherTable.hidden = YES;
    }
    
    _lineLayer.position = CGPointMake(sender.center.x, 37.5);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView.tag == 0) {
        return _schoolArr.count;
    }
    return _teacherArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView.tag == 0) {
        return 105;
    }
    return 95;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView.tag == 0) {
        
        static NSString *identity = @"SchoolListTableViewCell";
        SchoolListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identity];
        return cell;
    }
    
    static NSString *identity = @"TeacherListTableViewCell";
    TeacherListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identity];
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView.tag == 0) {
        
        SchoolListTableViewCell *scell = (SchoolListTableViewCell *)cell;
        SchoolListModel *model = _schoolArr[indexPath.row];
        [scell.schoolImageView sd_setImageWithURL:[NSURL URLWithString:model.cover]];
        scell.nameLabel.text = model.name;
        scell.addressLabel.text = [NSString stringWithFormat:@"%@%@", model.region, model.address_detail];
        scell.starView.value = model.grade;
    }else {
        
        TeacherListTableViewCell *tcell = (TeacherListTableViewCell *)cell;
        TeacherModel *model = _teacherArr[indexPath.row];
        [tcell.teacherImageView sd_setImageWithURL:[NSURL URLWithString:model.avatar]];
        tcell.nameLabel.text = model.name;
        if (model.gender == 0) {
            tcell.genderImageView.image = nil;
        }else if (model.gender == 1) {
            tcell.genderImageView.image = [UIImage imageNamed:@"gender_m"];
        }else {
            tcell.genderImageView.image = [UIImage imageNamed:@"gender_f"];
        }
        tcell.starView.value = model.grade;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView.tag == 0) {
        
        NormalWebViewController *controller = [[NormalWebViewController alloc] init];
        controller.url = _schoolArr[indexPath.row].link;
        [self.navigationController pushViewController:controller animated:YES];
    }else {
        
        TeacherDetailViewController *detail = [[TeacherDetailViewController alloc] init];
        detail.teacher = _teacherArr[indexPath.row];
        [self.navigationController pushViewController:detail animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
